<?php

namespace app\member\controller;


use think\Db;

/**
 * 积分兑换控制器
 */
class Integral extends Base
{
    public $noNeedLogin = [

    ];

    /**
     * 获取所有的商品和可兑换商品
     */
    public function getGoods () {

        if($this->request->isPost()) {
            $vip_type_id = $this->request->post('vip_type_id');

            if (!empty($vip_type_id)) {
                $where[] = ['','exp',Db::raw("FIND_IN_SET($vip_type_id,vip_type_id)")];
            }else {//获取所有商品
                $where = '1=1';
            }

            $integral = Db::name('qt_viper_integral_exchange')->field([
                'id',
                'integral',
                'money',
                'money',
                'vip_type_id',
                'goods_name',
                'img',
            ])->where($where)->select();

            $this->result('查询成功', $integral, 200);

        }

        $this->result('无效的请求', '', 204);


    }


    /**
     * 积分兑换商品
     */
    public function exchange() {

        //会员卡ID
        //商品数组['id'=>1]
        if($this->request->isPost()) {
            $post = $this->request->post();
            if (empty($post['card_id']) || empty($post['goods_ids']) || empty($post['viper_type_id'])) {
                $this->result('参数错误', '', 204);
            }

            $user_info = $this->user_info;

            $viper_card_score =  Db::name('qt_viper_card')->where('id',$post['card_id'])->value('score');
            if (empty($viper_card_score)) {
                $this->result('该卡没有可用积分了！', '', 204);
            }

            $goods_ids = explode(',',$post['goods_ids']);
            $integral = Db::name('qt_viper_integral_exchange')->whereIn('id',$goods_ids)->sum('integral');

            if ($integral > $viper_card_score ) {
                $this->result('该卡没有可用积分可供扣除了！', '', 204);
            }

            //更新卡积分
            Db::name('qt_viper_card')->where('id',$post['card_id'])->update(['score'=>intval($viper_card_score-$integral)]);

            $viper_card =  Db::name('qt_viper_card')->where('id',$post['card_id'])->find();
            $integral_data = Db::name('qt_viper_integral_exchange')->whereIn('id',$goods_ids)->select();

            $time = time();
            $data = [];
            //记录使用积分表
            foreach ($integral_data as $key => $val) {
                $data[] = [
                    'store_id'=>$user_info['store_id'],
                    'viper_type_id'=>$post['viper_type_id'],
                    'mobile'=>$user_info['phone'],
                    'name'=>$val['name'],
                    'card_no'=>$viper_card['card_no'] ?? 0,
                    'integral'=>$val['integral'],
                    'money'=>$val['money'],
                    'goods_name'=>$val['goods_name'],
                    'integral_id'=>$val['id'],
                    'user_id'=>$user_info['id'],
                    'create_time'=>$time,
                    'update_time'=>$time,
                ];
            }

            Db::name('qt_viper_integral_log')->insertAll($data);
            $this->result('兑换成功', $integral, 200);

        }

        $this->result('无效的请求', '', 204);

    }


    /**
     * 获取积分兑换记录列表
     */
    public function getIntegralLog () {

        if($this->request->isPost()) {

            $post = $this->request->post();
            $user_info = $this->user_info;
            $limit = !empty($post['limit']) ? $post['limit'] : 10;
            //if (empty($post['viper_type_id'])) {
            //    $this->result('会员卡类型不能为空', '', 204);
            //}
            $viper_card =  Db::name('qt_viper_card')->where('id',$post['card_id'])->find();
            if (empty($viper_card)) {
                $this->result('没有找到会员卡！', '', 204);
            }

            $where['il.user_id'] = $user_info['id'];
            $where['il.card_no'] = $viper_card['card_no'];
            if (!empty($post['is_receive']) && $post['is_receive'] == 2) {
                $where['il.is_receive'] = 2;
            }

            $integral = Db::name('qt_viper_integral_log')
                ->field([
                    'il.*',
                    'ie.img',
                ])
                ->alias('il')
                ->leftJoin('qt_viper_integral_exchange ie','il.integral_id=ie.id')
                ->where($where)
                ->paginate($limit);

            $this->result('查询成功', $integral, 200);

        }

        $this->result('无效的请求', '', 204);
    }



    /**
     * 领取积分兑换商品
     */
    public function receiveGoods () {

        if($this->request->isPost()) {

            $post = $this->request->post();
            $user_info = $this->user_info;
            if (empty($post['integral_log_id'])) {
                $this->result('积分兑换记录ID不能为空', '', 204);
            }

            $integral = Db::name('qt_viper_integral_log')
                ->where('user_id',$user_info['id'])
                ->where('id',$post['integral_log_id'])->find();
            if (empty($integral)) {
                $this->result('没有找到领取记录', '', 204);
            }

            Db::name('qt_viper_integral_log')
                ->where('user_id',$user_info['id'])
                ->where('id',$post['integral_log_id'])->update(['is_receive'=>1]);

            $this->result('领取成功', '', 200);
        }

        $this->result('无效的请求', '', 204);
    }



    /**
     * 会员兑换积分详情
     */
    public function detail () {
        if($this->request->isPost()) {
            $post = $this->request->post();
            if (empty($post['card_id'])) {
                $this->result('会员卡不能为空', '', 204);
            }

            $user_info = $this->user_info;

            $viper_card =  Db::name('qt_viper_card')->where('id',$post['card_id'])->find();
            if (empty($viper_card)) {
                $this->result('没有找到会员卡！', '', 204);
            }

            $integral_num = Db::name('qt_viper_integral_log')
                ->where('user_id',$user_info['id'])
                ->where('card_no',$viper_card['card_no'])->count();

            $receive_num = Db::name('qt_viper_integral_log')
                ->where('user_id',$user_info['id'])
                ->where('card_no',$viper_card['card_no'])->where('is_receive',2)->count();

            $this->result('查询成功', ['log_num'=>$integral_num,'receive_num'=>$receive_num], 200);
        }

        $this->result('无效的请求', '', 204);
    }



}
